home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / OCLIP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  5.3 KB  |  208 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /**
  5.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED 
  7.  * Permission to use, copy, modify, and distribute this software for 
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that 
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission. 
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  * 
  28.  * US Government Users Restricted Rights 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40. /*----------------------------------------------------------------------------
  41.  *
  42.  * oclip.c : openGL (motif) example showing how to use arbitrary clipping plane.
  43.  *
  44.  * Author : Yusuf Attarwala
  45.  *          SGI - Applications
  46.  * Date   : Mar 93
  47.  *
  48.  *    note : the main intent of this program is to demo the arbitrary
  49.  *           clipping functionality, hence the rendering is kept
  50.  *           simple (wireframe) and only one clipping plane is used.
  51.  *
  52.  *    press  left   button to move object 
  53.  *           right  button to move clipping plane
  54.  *
  55.  *
  56.  *---------------------------------------------------------------------------*/
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59.  
  60. #include <GL/glut.h>
  61.  
  62. /* function declarations */
  63.  
  64. void
  65.   drawScene(void), setMatrix(void), animateClipPlane(void), animation(void),
  66.   resize(int w, int h), keyboard(unsigned char c, int x, int y);
  67.  
  68. /* global variables */
  69.  
  70. float ax, ay, az;       /* angles for animation */
  71. GLUquadricObj *quadObj; /* used in drawscene */
  72. GLdouble planeEqn[] =
  73. {0.707, 0.707, 0.0, 0.0};  /* initial clipping plane eqn */
  74.  
  75. int count = 0;
  76. int clip_count = 0;
  77.  
  78. void
  79. menu(int choice)
  80. {
  81.   switch (choice) {
  82.   case 1:
  83.     count = 90;
  84.     glutIdleFunc(animation);
  85.     break;
  86.   case 2:
  87.     animateClipPlane();
  88.     break;
  89.   }
  90. }
  91.  
  92. int
  93. main(int argc, char **argv)
  94. {
  95.   glutInit(&argc, argv);
  96.  
  97.   quadObj = gluNewQuadric();  /* this will be used in drawScene 
  98.                                */
  99.   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  100.   glutCreateWindow("Arbitrary clip plane");
  101.  
  102.   ax = 10.0;
  103.   ay = -10.0;
  104.   az = 0.0;
  105.  
  106.   glutDisplayFunc(drawScene);
  107.   glutReshapeFunc(resize);
  108.   glutKeyboardFunc(keyboard);
  109.   glutCreateMenu(menu);
  110.   glutAddMenuEntry("Rotate", 1);
  111.   glutAddMenuEntry("Move clip plane", 2);
  112.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  113.   glutMainLoop();
  114.   return 0;             /* ANSI C requires main to return int. */
  115. }
  116.  
  117. void
  118. drawScene(void)
  119. {
  120.   glClearColor(0.0, 0.0, 0.0, 0.0);
  121.   glClear(GL_COLOR_BUFFER_BIT);
  122.  
  123.   glPushMatrix();
  124.   gluQuadricDrawStyle(quadObj, GLU_LINE);
  125.   glColor3f(1.0, 1.0, 0.0);
  126.   glRotatef(ax, 1.0, 0.0, 0.0);
  127.   glRotatef(-ay, 0.0, 1.0, 0.0);
  128.  
  129.   glClipPlane(GL_CLIP_PLANE0, planeEqn);  /* define clipping
  130.                                              plane */
  131.   glEnable(GL_CLIP_PLANE0);  /* and enable it */
  132.  
  133.   gluCylinder(quadObj, 2.0, 5.0, 10.0, 20, 8);  /* draw a cone */
  134.  
  135.   glDisable(GL_CLIP_PLANE0);
  136.   glPopMatrix();
  137.  
  138.   glutSwapBuffers();
  139. }
  140.  
  141. void
  142. setMatrix(void)
  143. {
  144.   glMatrixMode(GL_PROJECTION);
  145.   glLoadIdentity();
  146.   glOrtho(-15.0, 15.0, -15.0, 15.0, -10.0, 10.0);
  147.   glMatrixMode(GL_MODELVIEW);
  148.   glLoadIdentity();
  149. }
  150.  
  151. void
  152. animation(void)
  153. {
  154.   if (count) {
  155.     ax += 5.0;
  156.     ay -= 2.0;
  157.     az += 5.0;
  158.     if (ax >= 360)
  159.       ax = 0.0;
  160.     if (ay <= -360)
  161.       ay = 0.0;
  162.     if (az >= 360)
  163.       az = 0.0;
  164.     glutPostRedisplay();
  165.     count--;
  166.   }
  167.   if (clip_count) {
  168.     static int sign = 1;
  169.  
  170.     planeEqn[3] += sign * 0.5;
  171.     if (planeEqn[3] > 4.0)
  172.       sign = -1;
  173.     else if (planeEqn[3] < -4.0)
  174.       sign = 1;
  175.     glutPostRedisplay();
  176.     clip_count--;
  177.   }
  178.   if (count <= 0 && clip_count <= 0)
  179.     glutIdleFunc(NULL);
  180. }
  181.  
  182. void
  183. animateClipPlane(void)
  184. {
  185.   clip_count = 5;
  186.   glutIdleFunc(animation);
  187. }
  188.  
  189. /* ARGSUSED1 */
  190. void
  191. keyboard(unsigned char c, int x, int y)
  192. {
  193.   switch (c) {
  194.   case 27:
  195.     exit(0);
  196.     break;
  197.   default:
  198.     break;
  199.   }
  200. }
  201.  
  202. void
  203. resize(int w, int h)
  204. {
  205.   glViewport(0, 0, w, h);
  206.   setMatrix();
  207. }
  208.